package com.gitee.cirnochat.chat.service.stragegy;

import com.gitee.cirnochat.chat.model.vo.req.ChatMessageSendRequest;
import com.gitee.cirnochat.websocket.model.dto.WebSocketUserMessage;
import com.gitee.cirnochat.websocket.model.vo.resp.WebSocketMessageResponse;
import com.gitee.cirnochat.websocket.service.WebSocketService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import static com.gitee.cirnochat.common.constant.WebSocketConstant.CHAT_SUBSCRIBE_CHANNEL;

@Component("onlineSendStrategy")
@RequiredArgsConstructor
public class ChatMessageOnlineSendStrategy implements ChatMessageSendStrategy {

    private final WebSocketService webSocketService;

    @Override
    public WebSocketMessageResponse sendMessage(ChatMessageSendRequest req) {
        Long receiverId = req.getSessionId();
        WebSocketUserMessage message = new WebSocketUserMessage(
                receiverId.toString(),
                CHAT_SUBSCRIBE_CHANNEL,
                req
        );
        webSocketService.sendMessageToOnlineDevices(message);
        // 可根据实际需求返回响应
        return WebSocketMessageResponse.Builder
                .messageId(req.getMessageId())
                .message("消息已发送（在线）")
                .build();
    }
}